package integracao;
import java.sql.*;

public class Integracao {

    public static void main(String[] args) {
        
        String sqlrmed = "select codigo,editor from leditora";
        Conecta.abreConexao();
        ResultSet resed = Conecta.executaConsulta(sqlrmed);
        try {
            while(resed.next()){
                String sql = "select * from tb_editora where id_editora = " + resed.getInt("codigo");   
                ResultSet ressql = Conecta.executaConsulta(sql);
                
                    if(ressql.next()){
                        String up = "update tb_editora set editora = '"+ resed.getString("editor") + "' where id_editora = " + resed.getInt("codigo");
                        Conecta.executaSQL(up);
                    }else {
                        String in = "insert into tb_editora (id_editora,editora)  values ('"+ resed.getInt("codigo") +"', '" + resed.getString("editor") + "')";
                        Conecta.executaSQL(in);
                    }
                
                }   
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
          
    
        String sqlrmcat = "select codcoligada,codigo from lcategoria";
        Conecta.abreConexao();
        ResultSet rescat = Conecta.executaConsulta(sqlrmcat);
        try {
            while(rescat.next()){
                String sql = "select * from tb_categoria where id_categoia = " + rescat.getInt("codcoligada");   
                ResultSet ressql = Conecta.executaConsulta(sql);
                
                    if(ressql.next()){
                        String up = "update tb_categoria set categoria = '"+ rescat.getInt("codigo") + "' where id_categoria = " + rescat.getInt("codcoligada");
                        Conecta.executaSQL(up);
                    }else {
                        String in = "insert into tb_categoria (id_categoria,categoria)  values ('"+ rescat.getInt("codcoligada") +"', '" + rescat.getInt("codigo") + "')";
                        Conecta.executaSQL(in);
                    }
                
                }   
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        
        String sqlrmaut = "select codigo,nome from lautor";
        Conecta.abreConexao();
        ResultSet resaut = Conecta.executaConsulta(sqlrmaut);
        try {
            while(resaut.next()){
                String sql = "select * from tb_autor where id_autor=" + resaut.getInt("codigo");
                ResultSet ressql = Conecta.executaConsulta(sql);
                
                if(ressql.next()){
                    String up = "update tb_autor set autor='"+resaut.getString("nome")+"' where id_autor='"+resaut.getInt("codigo")+"'";
                    Conecta.executaSQL(up);
                } else {
                    String in = "insert into tb_autor (codigo, autor) values ('"+resaut.getInt("codigo")+"', '"+resaut.getString("nome");
                    Conecta.executaConsulta(in);
                }
            }
        } catch (SQLException ex){
            System.out.println(ex.getMessage());
        }
        
        String sqlrmtip = "select distinct areacursodepto from lpublic";
        Conecta.abreConexao();
        ResultSet resas = Conecta.executaConsulta(sqlrmtip);
        try {
            String sql = "select * from tb_assunto where assunto="+resas.getString("areacursodepto");
            ResultSet ressql = Conecta.executaConsulta(sql);
            if(ressql.next()){
                String up = "update tb_assunto set assunto='"+resas.getString("areacursodepto")+"'";
                Conecta.executaSQL(up);
            } else {
                String in = "insert into tb_assunto (assunto) values('"+resas.getString("areacursodepto")+"')";
                Conecta.executaSQL(in);
            }
        } catch (SQLException ex){
            System.out.println(ex.getMessage());
        }
    }
}